//
//  SDVerificationCodeButton.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/30.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDVerificationCodeButton.h"

#define normalTitle @"获取验证码"

@interface SDVerificationCodeButton ()

@property(nonatomic ,strong)NSTimer *timer;

@end
@implementation SDVerificationCodeButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    
    if (self) {
        
        self.layer.cornerRadius = 12.5;
        self.layer.masksToBounds = YES;
        self.titleLabel.font = Font(13);
        
        [self setTitleColor:SDWhiteColor                                                                                                                                         forState:UIControlStateNormal];
         self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime:) userInfo:nil repeats:YES];
        self.hasCode = NO;
        self.use = NO;
        [self.timer setFireDate:[NSDate distantFuture]];
        [self setBackgroundColor:RGBA(246, 206, 98, 1)];
        self.alpha = 0.5;
        
    }
   
    return self;
    
}
- (void)changeTime:(NSTimer *)time
{
    
    self.time = self.time - 1;
    [self setTitle:[NSString stringWithFormat:@"%lds",(long)self.time] forState:UIControlStateNormal];
    if (self.time <= 0) {
        
        self.hasCode = NO;
        self.use = self.phone.length == 11;
       
    }
    
    
}
#pragma mark - setter方法
- (void)setUse:(BOOL)use
{
    _use = use;
   self.userInteractionEnabled = use;
   self.alpha = use == YES? 1.0:0.5;
    
    if (!use&&self.phone.length == 11) {
        
        self.time = 60;
        [self setTitle:[NSString stringWithFormat:@"%lds",(long)self.time] forState:UIControlStateNormal];
        [self.timer setFireDate:[NSDate date]];
        
    }else if(self.hasCode == NO){
        
        [self.timer setFireDate:[NSDate distantFuture]];
        [self setTitle:normalTitle forState:UIControlStateNormal];
        
    }
    
}
@end
